/**
 * @name 权限
 */
layui.define(['larry', 'form', 'table'], function(exports) {
    "use strict";
    var $ = layui.$,
        larry = layui.larry,
        form = layui.form,
        table = layui.table,
        larryms = layui.larryms;
    //ztree
    
    larryms.plugin('/ztree/js/jquery.ztree.all.js', statistics);
    var zTreeObj;
    var treeObj;
    function statistics() {
        var setting = {
            data: {
                simpleData: {
                    enable: true,
                }
            },
            check: {
                enable: true
            },
            callback: {
                onClick: function(e, treeId, treeNode, clickFlag) {
                    zTreeObj.checkNode(treeNode, !treeNode.checked, true);
                    $("#zTreeResults").val(zTreeResult());
                }
            }
        }
        var $j = jQuery.noConflict();
        var zNodes = "";
        //选择树
        function choice_ztree(){
            $.post("/backend/system/upms/role/selectRolePermissionByRoleId",{"roleId":GetQueryString("roleId")},function(result){
            		for ( var i = 0; i < result.data.length; i++) {
            			
    					var node = treeObj.getNodeByParam("id",result.data[i].permissionId,
    							null);
    					if (node) {
    						
    						if (!node.getCheckStatus().half
    								&& !node.isParent) {
    							treeObj.checkNode(node, true, true);
    						}
    						
    						
    						
    					}
    				}
            });
        }
        
        //初始化加载节点
        function Inint() {
            $.ajax({
                global: true,
                async: false,
                cache: false,
                dataType: "json",
                url: '/backend/system/upms/permissions/selectPermissions',
                type: "post",
                success: function(result) {
                    zNodes = result.data;
                    zTreeObj =  $j.fn.zTree.init($("#treeDemo"), setting, zNodes);
                    treeObj = $j.fn.zTree.getZTreeObj("treeDemo");
                    var nodes = treeObj.getNodes();
                    for (var i = 0; i < nodes.length; i++) { //设置节点展开
                        treeObj.expandNode(nodes[i], true, true, true);
                    }
                    choice_ztree()
                }
            });
        };
        Inint(); //加载数据
    }
    
  //保存
	$('#btnSubmit').on('click', function() {
		var checkedNodes = treeObj.getCheckedNodes(true);
		var checkArray = new Array();
		for ( var i = 0; i < checkedNodes.length; i++) {
				checkArray.push(checkedNodes[i].id);
		}
		var checkAppList = checkArray.join(",");
		
		$.post("/backend/system/upms/role/addRolePermission",{"roleId":GetQueryString("roleId"),"permissionId":checkAppList},function(result){
			if(result.status != 200){
				layer.msg(result.msg);
			} else {
				parent.layer.closeAll();
			}
		});
	});
    
	
	$('#close').on('click', function() {
		parent.layer.closeAll();
	});
    
    exports('mypanel', {});
});